<?php
class CartPanel_ZoneController extends Ext_Controller_SecureAction
{
    public function listAction()
	{
      	$data = new CartWS_Model_Zone();
       	$this->view->paginator = new Ext_Paginator( 
       		$data->getSelectWithDescr( 'country' ), 
       		$this->getRequest()->getParam('page') );
       	$this->view->childName = 'region';
       	$this->view->parentId = 0;
       	$this->view->zone = null;
    }
    
    public function regionlistAction()
    {
       	$data = new CartWS_Model_Zone();
       	$this->view->paginator = new Ext_Paginator( 
       		$data->getSelectWithDescr( 'region', 
       		$this->getRequest()->getParam('parentId') ) 
       	);
       	$this->view->parentName = '';
       	$this->view->zone = 'county';
       	$this->view->parentId = $this->getRequest()->getParam('parentId');
       	$this->view->childName = 'county';
       	$this->_helper->getHelper( 'viewRenderer')->setScriptAction( 'list' );
    }
    
    public function countylistAction()
    {
       	$data = new CartWS_Model_Zone();
       	$this->view->paginator = new Ext_Paginator( 
       		$data->getSelectWithDescr( 'county', $this->getRequest()->getParam('parentId'), 
       		$this->getRequest()->getParam('page')  ) 
       	);
       	$this->view->parentParent = $data->findParentParent( 
       		$this->getRequest()->getParam('parentId') 
       	);
       	$this->view->parentName = 'region';
       	$this->view->zone = 'county';
       	$this->view->parentId = $this->getRequest()->getParam('parentId');
       	$this->view->childName = 'city';
       	$this->_helper->getHelper( 'viewRenderer')->setScriptAction( 'list' );
   	}

    public function createAction()
    {
        $request = $this->getRequest();
        $form = new CartPanel_Form_Zone();
        $form->populate(
            array( 
        		'parent_zone_id' => $request->getParam( 'parentId' )
        	)
        );
        if ($this->getRequest()->isPost()) {
            if ($form->isValid($request->getPost())) {
                $model = new CartWS_Model_Zone();
                $row = $model->createRow(  $form->getValues()  );
                $row->save();
                
				$urlBack = $this->view->url(
	                array(
	                	'controller'=>'zone', 
	                	'action' => $request->getParam( 'zone' ).'list', 
	            	 	'parentId' => $request->getParam( 'parentId' ),
	                ) 
	            );
            	return $this->_redirect( $urlBack );
            }
        $this->view->form = $form;
        }
    }

    public function updateAction()
    {
        $request = $this->getRequest();
        $form = new CartPanel_Form_Zone();
        $form->populate(
            array( 
        		'parent_zone_id' => $request->getParam( 'parentId' )
        	)
        );
        
        $model = new CartWS_Model_Zone();
		$data = $model->find( $request->id )->current()	;
        if ($this->getRequest()->isPost()) {
        	
            if ($form->isValid( $request->getPost() ) ) {
                $data->name = $form->getValue( 'name' );
                $data->shipping_zone_id = $form->getValue( 'shipping_zone_id' );
                
                $data->save();
				$urlBack = $this->view->url(
	                array(
	                	'controller'=>'zone', 
	                	'action' => $request->getParam( 'zone' ).'list', 
           	 			'parentId' => $request->getParam( 'parentId' ),
	                ) 
	            );
            	return $this->_redirect( $urlBack );
        	}
        } else {
        	$form->populate( $data->toArray() );
        }
        $this->view->form = $form;
    }
    
    public function deleteAction()
    {
    	$request = $this->getRequest();
		$model = new CartWS_Model_Zone();
		$row = $model->find( $this->getRequest()->id )->current();
		if( $row != null ) {
			$row->locked =  ( $row->locked == 'T' ) ? 'F' : 'T';
			$row->end_date = ( $row->locked == 'T' ) ? Ext_Db_Date::now() : null ;
			$row->save();
		}
		$urlBack = $this->view->url(
	    	array(
	         	'controller'=>'zone', 
	           	'action' => $request->getParam( 'zone' ).'list', 
	         	'parentId' => $request->getParam( 'parentId' ),
	        )
		);
        return $this->_redirect( $urlBack );
		
    }
}
